package com.zhaodui.restapi.base;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.zhaodui.jdwx.jdwxController;
import com.zhaodui.order.entity.FxjOrderAttaEntity;
import com.zhaodui.order.page.FxjOrderMainPage;
import com.zhaodui.pa.entity.FxjPaInsureEntity;
import com.zhaodui.restapi.entity.FileMeta;
import com.zhaodui.restapi.entity.FxjoutputEntity;
import com.zhaodui.restapi.entity.fxjMenuEntity;
import com.zhaodui.util.QRcodeUtil;
import com.zhaodui.util.fxjUtil;
import com.zhaodui.util.printUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import org.jeecgframework.web.system.pojo.base.*;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Validator;
import java.awt.image.BufferedImage;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

import static com.xiaoleilu.hutool.date.DateTime.now;


@Api(value="basebiRest",description="bi",tags="basebiRest")
@Controller
@RequestMapping(value = "/basebi")
public class BasebiController {
    @Autowired
    UserService userService;
    @Autowired
    SystemService systemService;
	@Autowired
	private Validator validator;

	@ApiOperation(value="getbi",produces="application/json",httpMethod="GET")
	@RequestMapping(value="/getbi/{username}", method = RequestMethod.GET)
	public @ResponseBody ResponseMessage<List<FxjoutputEntity>>  getbi(@RequestParam(value = "pageNumber" ,required = false) int pageNumber, @RequestParam(value = "pageSize" ,required = false) int pageSize,@ApiParam(required=true,name="username",value="username")@PathVariable("username") String username, @RequestParam(value="bitype", required=false) String bitype, @RequestParam(value="consql", required=false) String consql, HttpServletRequest request) {
		String  orgcode = "no";
		if (StringUtil.getStringLen(username)>20){

		}else {
			TSUser task = fxjUtil.getsysorgcode(username);
			if (task != null) {
				orgcode = task.getCurrentDepart().getSysOrgCode();

			}
		}
		List<FxjoutputEntity> task = new ArrayList<>();
       String csql="";
        String sql = "select     cast(sum(loan_amount)  as signed) as loan_amount," +
				"    count(*) as jianshu,    cast((select   (sum(loan_amount))    " +
				"    FROM            fxj_loan_fk        where          " +
				"  to_days(create_date) = to_days(now())) as signed) as day_amount, " +
				"   CAST(((select                     count(*)                from     " +
				"               fxj_loan_fk                where           " +
				"         bpm_status = 80) / (select                     count(*)   " +
				"     from      fxj_order_main) * 100) as signed) as bili from  " +
				"   fxj_loan_fk where    bpm_status = 80"  ;
        long total= 0;
		if ("bihome".equals(bitype)){
			List<Map<String, Object>> resultmap	= systemService.findForJdbc(sql);
			FxjoutputEntity  fxjoutputEntity = new FxjoutputEntity();
			Map<String, Object> map;
			Double count = 0.00;
			if (resultmap.size() > 0) {
				for (Map<String,Object>  object : resultmap) {
					Map<String,Object> obj =    object;
					try{
          try{
			  if(StringUtil.isEmpty(obj.get("loan_amount").toString())){
				  fxjoutputEntity.setFxjx1("0");

			  }else{
				  fxjoutputEntity.setFxjx1(obj.get("loan_amount").toString());

			  }
		  }catch (Exception e){
			  fxjoutputEntity.setFxjx1("0");

		  }
						try{
							if(StringUtil.isEmpty(obj.get("day_amount").toString())){
								fxjoutputEntity.setFxjx2("0");

							}else{
								fxjoutputEntity.setFxjx2(obj.get("day_amount").toString());

							}
						}catch (Exception e){
							fxjoutputEntity.setFxjx2("0");

						}
						try{
							if(StringUtil.isEmpty(obj.get("jianshu").toString())){
								fxjoutputEntity.setFxjx3("0");

							}else{
								fxjoutputEntity.setFxjx3(obj.get("jianshu").toString());

							}
						}catch (Exception e){
							fxjoutputEntity.setFxjx3("0");

						}
						try{
							if(StringUtil.isEmpty(obj.get("bili").toString())){
								fxjoutputEntity.setFxjx4("0");

							}else{
								fxjoutputEntity.setFxjx4(obj.get("bili").toString()+"%");

							}
						}catch (Exception e){
							fxjoutputEntity.setFxjx4("0");

						} try{
							if(StringUtil.isEmpty(obj.get("bili").toString())){
								fxjoutputEntity.setFxjx5("0");

							}else{
								fxjoutputEntity.setFxjx5(obj.get("bili").toString());
							}

						}catch (Exception e){
							fxjoutputEntity.setFxjx5("0");

						}


					}catch (Exception e){

					}
				}
			}
			task.add(fxjoutputEntity);
//			task = this.getlistbyparandrable(resultmap);
		} else{
			String   rpsql=ResourceUtil.getConfigByName("fxj.report."+bitype);
			String   rcsql=ResourceUtil.getConfigByName("fxj.count."+bitype);
			List<Map<String, Object>> resultmap	= systemService.findForJdbcParam(rpsql,pageNumber,pageSize);
			total = systemService.getCountForJdbc(rcsql);
			task = this.getlistbyparandrable(resultmap);
		}

		return Result.success(task,total);
	}

	private  List<FxjoutputEntity> getlistbyparandrable(List<Map<String, Object>>  resultmap){
		List<FxjoutputEntity> reslit = new ArrayList<FxjoutputEntity>();
		for(int i=0;i<resultmap.size();i++){
			Map<String, Object>   itermap = resultmap.get(i);
			Iterator<Map.Entry<String, Object>> entries = itermap.entrySet().iterator();
			try {
			Class cl = Class.forName("com.zhaodui.restapi.entity.FxjoutputEntity");//反射得到类
			Object obj = cl.newInstance();//新建一个实例
			int j = 0;
			while (entries.hasNext()) {
				Map.Entry<String, Object> entry = entries.next();
				j++;
				String methodstr = "setFxjx"+j;
				Method method = cl.getMethod(methodstr,String.class);
				System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
				String svalue = "";
				try{
				 svalue = entry.getValue().toString();
				}catch (Exception e){
					e.printStackTrace();
				}
				if(StringUtil.isNotEmpty(svalue)){
					method.invoke(obj,svalue);
				}
			}
				FxjoutputEntity	t = (FxjoutputEntity) obj;
				reslit.add(t);
			}catch (Exception e){
				 e.printStackTrace();
			}
		}
		return reslit;

	}

}
